<template>
    <!--flex弹性盒子模型，justify-content：主抽 -->
    <div style="display: flex; justify-content: center;">
      <div class="background">
        <img :src="imgSrc" width="100%" height="100%" alt="" />
      </div>
      <el-card style="width: 400px; margin: 1%;">
        <div slot="header" class="clearfix">
          <span class="login-title">轻听音乐后台管理系统</span>
        </div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="60px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="userId">
            <el-input type="text" v-model="ruleForm.userId" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-row>
            <el-checkbox v-model="checked" style="padding-top: 12px; color: #409EFF;">记住用户名</el-checkbox>
            <el-button type="text" style="float: right"  @click="resetPwd">忘记密码<i class="el-icon-user el-icon--right"></i></el-button>
          </el-row>
          <el-row><el-button type="primary" class="sub" @click="doLogin('ruleForm')">立即登录</el-button></el-row>
          <el-row style="float: right">
            <el-button type="text" style="float: right" @click="doRegister">还没注册，请前往注册<i class="el-icon-user-solid el-icon--right"></i></el-button>
          </el-row>
        </el-form>
      </el-card>
    </div>
</template>

<script>

import Vue from 'vue'
import { Button, Form, FormItem, Input, Message } from 'element-ui'
import { loginIn } from '../../api/index'

Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.prototype.$message = Message
export default {
  name: 'Login',
  data: function () {
    var checkName = function (rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    var validatePass = function (rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        password: '',
        userId: ''
      },
      rules: {
        userId: [
          { validator: checkName, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ]
      },
      // imgSrc: require('../../assets/img/background.jpg'),
      imgSrc: require('../../assets/img/music.jpg'),
      checked: true
    }
  },
  methods: {
    doLogin: function (formName) {
      var that = this
      that.$refs[formName].validate(function (valid) {
        if (valid) {
          if (that.checked) {
            localStorage.setItem('userId', that.ruleForm.userId)
          } else {
            localStorage.removeItem('userId')
          }
          var params = {}
          params.userId = that.ruleForm.userId
          params.passWord = that.ruleForm.password
          loginIn(params)
            .then(res => {
              // console.log('-----------获取登录信息---------------')
              if (res.code === '000000') {
                that.$message({
                  message: '登录成功',
                  type: 'success'
                })
                that.$router.push({path: '/page/index/index'})
              } else {
                that.$message({
                  message: res.message,
                  type: 'warning'
                })
              }
            })
            .catch(failResponse => {})
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 注册
    doRegister: function () {
      this.$router.push({path: '/page/register/register'})
    },
    // 忘记密码
    resetPwd: function () {
      this.$router.push({path: '/page/resetPwd/resetPwd'})
    }
  },
  mounted: function () {
    this.ruleForm.userId = localStorage.getItem('userId')
  }
}
</script>

<style scoped>
.background{
    width:30%;
    z-index: -1;
    margin-top: 1%;
    margin-bottom: 1%;
    float: left;
    position: relative;
}
.login-title{
  font-weight: bold;
  color: #409EFF;
}
.el-button--primary.sub{
  width: 50%;
}
</style>
